// void, Obj me, Obj h

Unit this, y;
Hero hero;
point ptoffset, ptEnter;
Obj teleport;
bool LimitHorse;
int i, limit, admin, horse;

Sleep(rand(100)+30);
hero = h.AsHero();
this = me.AsUnit();

if (.IsValid) {// leave parry mode at new command
	.SetParryMode(false);
	
	if(.hero.IsValid){
		if(.hero.HasItem("irage_battle")){
			Sleep(rand(200)+50);
			.AddCommand(true, "engage");
			return;
		} else if(.hero.HasItem("irage_battle_stand")){
			Sleep(rand(200)+50);
			.AddCommand(true, "stand_position");
			return;
		}
	}
	
	if(.item_count > 0){
		Item item;
		
		item = .FindItem("Terrain bonus I");
		if(item.IsValid)
			.RemoveItemsOfType("Terrain bonus I");
		else {
			item = .FindItem("Terrain bonus II");
			if(item.IsValid)
				.RemoveItemsOfType("Terrain bonus II");
			else {
				item = .FindItem("Terrain bonus III");
				if(item.IsValid)
					.RemoveItemsOfType("Terrain bonus III");
				else {
					item = .FindItem("Terrain bonus IV");
					if(item.IsValid)
						.RemoveItemsOfType("Terrain bonus IV");
				}
			}
		}
	}
}

Breakpoint();

if(me == h) return;
if(!hero.IsValid()) return;
if(!hero.IsAlive()) return;
if(this.IsEnemy(hero)) return;
if(.IsHeirOf("ShipBattle")) return;
if(.IsHeirOf("Wagon")) return;
if(.IsHeirOf("ShipTransport")) return;
if(.IsHeirOf("Catapult")) return;
if(.IsHeirOf("Animal")) return;
if(.IsHeirOf("Sentry")) return;
if(.IsHeirOf("SentryOutpost"))
	if(!hero.IsHeirOf("CaptainOutpost")) return;

if(.HasFreedom){
	if(.AsHero.IsValid)
        return;
	
	if(!h.HasItem("Leader")){
		Sleep(rand(100)+30);
		.AddCommand(true, "protect", hero);
		.AddCommand(true, "approach", hero);
		return;
	}
}
//if (this.AsHero.IsValid()) return; 
ptoffset.Set(rand(600) - 300, rand(600) - 300);
//.pr("attaching to hero");
//if(.IsValid)
//	if(.IsMilitary)
		//if(.speed_factor < 100)                                  
			//.SetSpeedFactor(100);

LimitHorse = true;

while(1){
	
	if(!.IsHeirOf("TValkyrie"))
	if(.IsHeirOf("Horse") || .IsHeirOf("RangedHorse")){
		admin = (hero.GetSkill(hsAdministration)*GetConst("UnitsPerAdministrationLevel"));
		limit = hero.maxarmy+admin-20;
		horse = 0;
		for (i = 0; i<hero.army.count; i+=1){	
			y = hero.army[i].AsUnit();
			if(y.IsValid()){
				if(!y.IsHeirOf("TValkyrie"))
				if(y.IsHeirOf("Horse") || y.IsHeirOf("RangedHorse")){
					horse+=1;
				}
			}
		}
		if(horse >= limit) LimitHorse = false;
		else LimitHorse = true;
	}		
	//pr("attaching to hero");
	if(!hero.IsAlive()) break;//fail
	if(.IsEnemy(hero)) break;//fail
	if(hero.IsEnemy(this)) break;//fail

	if(LimitHorse)
	if(.SameHolderAs(hero)){
		if(.AttachTo(hero)){
			if(hero.formation == "Wings"){
				if(.IsMilitary)
					if(!.HasItem("Wings formation"))
						.AddItem("Wings formation");
			}
			break; // success
		}
	}

	if(.InShip) break; // fail
	if(hero.InShip) break; // fail
	if(LimitHorse)
	if(!.InHolder && !hero.InHolder && .posRH.Dist(hero.posRH) < 1500)
		if(.AttachTo(hero)){
			if(hero.formation == "Wings"){
				if(.IsMilitary)
					if(!.HasItem("Wings formation"))
						.AddItem("Wings formation");
			}
			break; // success
		}
	
	teleport = FindTeleport(.player, .posRH, hero.posRH);
	if(!.AI) if(teleport.IsValid) if(.posRH.Dist(teleport.pos) > 1000) teleport.Clear;
	if(teleport.IsValid){
		Sleep(rand(100)+30);
	  	.AddCommand(true, "attach", hero);
  		.AddCommand(true, "teleport", teleport);
		return;
	}
	if(hero.InHolder) 
		if(!hero.GetHolderSett.GetCentralBuilding.IsHeirOf("BaseRuins")){
			Building bld;
			
			bld = hero.GetHolderSett().GetCentralBuilding();
			if (!bld.IsValid()) break;//fail
			
			ptEnter = bld.GetEnterPoint(this);
			.SetEntering(true);
			
			while(!.GotoEnter(ptEnter, 0, 1500, true, 2500) && hero.InHolder());
			.SetEntering(false);
			if(hero.InHolder())
			{
				hero.GetHolderSett().AddUnit(this);
				if(hero.IsHeroArmyFull() || !LimitHorse) break;//fail
				continue;
			}
		}
		else
			Sleep( 100 );                        

	if(hero.IsHeroArmyFull() || !LimitHorse){
		Sleep(rand(100)+30);
		.AddCommand(true, "protect", hero);
		.AddCommand(true, "approach", hero);
		break;//fail
		
	}
	
	if(.Goto(hero.posRH + ptoffset, 1, 1500, true, 5000)){
	
		ptoffset.Set(0, 0);
	}
}

while(!.Stop(1000));
